package com.example.jpatest.service.impl;

import com.example.jpatest.entity.Enter;
import com.example.jpatest.entity.Teacher;
import com.example.jpatest.entity.Team;
import com.example.jpatest.entity.TeamTeacher;
import com.example.jpatest.repository.TeacherRepository;
import com.example.jpatest.repository.TeamRepository;
import com.example.jpatest.repository.TeamTeacherRepository;
import com.example.jpatest.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
 * @author czx
 */
@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    TeacherRepository teacherRepository;

    @Autowired
    TeamTeacherRepository teamTeacherRepository;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void saveTeacher(Teacher teacher) {
        teacherRepository.save(teacher);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void joinTeam(Teacher teacher, Team team, TeamTeacher.TeacherLevel teacherLevel) {
        TeamTeacher teamTeacher = new TeamTeacher();
        teamTeacher.setTeacherLevel(teacherLevel);
        teamTeacher.setTeacher(teacher);
        teamTeacher.setTeam(team);
        teamTeacherRepository.save(teamTeacher);
    }

    @Override
    public Teacher getById(Long id) {
        return teacherRepository.findById(id).orElse(null);
    }

    @Override
    public List<Team> getTeams(Teacher teacher) {
        List<Team> teams = new LinkedList<>();
        for (TeamTeacher teamTeacher : teacher.getTeamTeachers()) {
            Team team = teamTeacher.getTeam();
            if (team != null) {
                teams.add(teamTeacher.getTeam());
            }
        }
        return teams;
    }

    @Override
    public List<Teacher> getTeachers() {
        return teacherRepository.findAll();
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void deleteTeacher(Teacher teacher) {
        for (TeamTeacher teamTeacher : teacher.getTeamTeachers()) {
            teacher.getTeamTeachers().remove(teamTeacher);
            teamTeacherRepository.delete(teamTeacher);
        }
        teacherRepository.delete(teacher);
    }

    @Override
    public boolean isInTeam(Teacher teacher, Team team) {
        for (TeamTeacher teamTeacher : teacher.getTeamTeachers()) {
            if (teamTeacher.getTeam().equals(team)) {
                return true;
            }
        }
        return false;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void quitTeam(Teacher teacher, Team team) {
        for (TeamTeacher teamTeacher : teacher.getTeamTeachers()) {
            if(teamTeacher.getTeam().equals(team)){
                teacher.getTeamTeachers().remove(teamTeacher);
                teamTeacherRepository.delete(teamTeacher);
            }
        }
    }

    @Override
    public TeamTeacher.TeacherLevel getTeacherLevel(Teacher teacher, Team team) {
        for (TeamTeacher teamTeacher : teacher.getTeamTeachers()) {
            if (teamTeacher.getTeam().equals(team)) {
                return teamTeacher.getTeacherLevel();
            }
        }
        return null;
    }

    @Override
    public List<Enter> getEnters(Teacher teacher) {
        List<Enter> enters = new LinkedList<>();
        for (Team team : getTeams(teacher)) {
            enters.addAll(team.getEnter());
        }
        return enters;
    }
}
